package com.siwei.eqi.pojo.entity;

import lombok.Data;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;

@Data
public class RegionEvaluation {
    private static final BigDecimal A_VEG = new BigDecimal("121.1651"); // 归一化系数
    private static final int SCALE = 4;      // 统一精度
    private static final RoundingMode ROUND_MODE = RoundingMode.HALF_UP;

    private BigDecimal vegetationCoverIndex;  // 植被覆盖指数
    private BigDecimal windSandFixationIndex; // 防风固沙指数
    private BigDecimal soilConservationIndex;  // 水土保持指数

    public void calculateIndices(
            List<PixelData> pixels,
            BigDecimal nppMaxWind,   // 防风固沙用NPP最大值
            BigDecimal nppMaxSoil    // 水土保持用NPP最大值
    ) {
        int n = pixels.size();
        BigDecimal totalPixels = new BigDecimal(n);

        // 植被覆盖指数
        BigDecimal sumPj = pixels.stream()
                .map(PixelData::calculatePj)
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        this.vegetationCoverIndex = sumPj
                .divide(totalPixels, SCALE, ROUND_MODE)
                .multiply(A_VEG)
                .divide(new BigDecimal("10000"), SCALE, ROUND_MODE);

        // 防风固沙指数
        BigDecimal sumWind = pixels.stream()
                .map(p -> calculateWindIndexPerPixel(p, nppMaxWind))
                .reduce(BigDecimal.ZERO, BigDecimal::add);
        this.windSandFixationIndex = sumWind.divide(totalPixels, SCALE, ROUND_MODE);

        // 水土保持指数（逻辑类似，略）
    }

    private BigDecimal calculateWindIndexPerPixel(PixelData p, BigDecimal nppMax) {
        BigDecimal ndviPart = p.getNdviYearlyMax()
                .subtract(new BigDecimal("0.05"))
                .divide(new BigDecimal("0.70"), SCALE, ROUND_MODE);

        BigDecimal nppPart = p.getNppYearlySum()
                .divide(nppMax, SCALE, ROUND_MODE);

        return new BigDecimal("100")
                .multiply(
                        ndviPart.add(nppPart).divide(new BigDecimal("2"), SCALE, ROUND_MODE)
                );
    }
}